var app = getApp()
// index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        curIndex:1,
        attenderBox:"attender",
        swiftIcon:"down",
        latitude:0,
        longitude: 0,
        beginTimeUnix: Math.round(new Date().getTime()/1000),
        tabIcon:[{
          url:"icon-like",
            num:456
        },{
            url:"icon-comment",
            num:123
        },{
            url:"icon-album",
            num:45
        }
        ],
        imgArr:[{
          imgUrl:"../../images/theme1.jpg"
        },{
          imgUrl: "../../images/theme1.jpg"
          }, {
            imgUrl: "../../images/theme1.jpg"
        }, {
          imgUrl: "../../images/theme1.jpg"
        }]
    },
    __stpannel__clickText: function(e) {
        this.setData({curIndex: e.target.id});
        console.log(e.target.id);
    },

    showMap:function (e) {
        wx.openLocation({
            name:this.data.locationName,
            name:this.data.locationAddress,
            latitude:this.data.latitude,
            longitude: this.data.longitude,
            status:-1,
            isOrganiser:0,
            scale: 28
        })
    },
    attenderOpen:function () {
        if (this.data.swiftIcon=='down'){
            this.setData({
                attenderBox:"attender-long",
                swiftIcon:"up"
            })
        }else {
            this.setData({
                attenderBox:"attender",
                swiftIcon:"down"
            })
        }

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        this.$wuxToast = app.wux(this).$wuxToast
        this.setData({
            partyId:options.partyId
        })
        //调用应用实例的方法获取全局数据
        app.getUserInfo(function(userInfo){
            //更新数据
            that.setData({
                userInfo:userInfo
            })
            wx.request({
                url:"https://wxapps.lovemojito.com/mApp/getParty.php",
                data: {
                    openid:userInfo.openId,
                    partyId:options.partyId,
                    flag:0,
                    jyj:'jyj'
                },
                header: {
                    'content-type': 'application/json'
                },
                success: function(res) {
                    // console.log(res.data)
                    var latitude = res.data.latitude
                    var longitude = res.data.longitude
                    var markers = [{
                        iconPath: "../../images/location.png",
                        id: 0,
                        latitude:latitude,
                        longitude: longitude,
                        width: 30,
                        height: 30
                    }];
                    that.setData({
                        latitude:latitude,
                        longitude: longitude,
                        markers:markers,
                        title:res.data.title,
                        beginTime:res.data.beginTime,
                        beginTimeUnix:res.data.beginTimeUnix,
                        description:res.data.description,
                        locationName : res.data.locationName,
                        locationAddress: res.data.locationAddress,
                        status: res.data.status,
                        isOrganiser:res.data.isOrganiser,
                        isAttender:res.data.isAttender,
                        attender:res.data.attender,
                        attenderNum:res.data.attenderNum,
                        maxMember:res.data.maxMember
                    })
                    console.log(1)
                    console.log(that.data)
                    console.log(2)
                }
            })

        })

    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {
        return {
            title: this.data.userInfo.nickName+'邀请您加入聚会',
            path: '/pages/index/index?partyId='+this.data.partyId,
            success: function(res) {
                // 转发成功
            },
            fail: function(res) {
                // 转发失败
            }
        }
    },

    attend:function (options) {
        wx.navigateTo({
            url: '/pages/attend/attend?partyId='+ this.data.partyId + '&beginTimeUnix='+ this.data.beginTimeUnix +'&maxMember='+ this.data.maxMember
        })
    },
    cancelAttend:function (e) {
        var _this = this;
        var formId = e.detail.formId;
        console.log(formId)
        wx.request({
            url:"https://wxapps.lovemojito.com/mApp/attendOrCancel.php",
            data: {
                openid:this.data.userInfo.openId,
                partyId:this.data.partyId,
                formId:formId,
                flag:0,
                jyj:'jyj'
            },
            header: {
                'content-type': 'application/json'
            },
            success:function (res) {
                console.log(res)
                if(res.data.errCode == 1){
                    _this.$wuxToast.show({
                        type: 'success',
                        timer: 1500,
                        color: '#fff',
                        text: res.data.errMsg,
                        success:function () {
                            wx.navigateTo({
                                url: '/pages/index/index?partyId='+res.data.partyId
                            })
                        }
                    })
                }else {
                    _this.$wuxToast.show({
                        type: 'cancel',
                        timer: 1500,
                        color: '#fff',
                        text: res.data.errMsg
                    })
                }
            }

        })

    },
    reviseRemind:function () {
        wx.navigateTo({
            url: '/pages/attend/attend?partyId='+ this.data.partyId + '&beginTimeUnix='+ this.data.beginTimeUnix +'&maxMember='+ this.data.maxMember
        })
    },
    edit:function () {
        wx.navigateTo({
            url: '/pages/edit/edit?partyId='+ this.data.partyId + '&title='+ this.data.title +'&description='+ this.data.description
            +'&beginTime='+this.data.beginTime+'&beginTimeUnix='+this.data.beginTimeUnix+'&maxMember='+this.data.maxMember+'&attenderNum='+this.data.attenderNum
            +'&locationName='+this.data.locationName+'&locationAddress='+this.data.locationAddress+'&latitude='+this.data.latitude+'&longitude='+this.data.longitude
        })
    },
    /**
     * 下拉刷新
     */
    onPullDownRefresh: function(){
        var that = this
        this.$wuxToast = app.wux(this).$wuxToast

        //调用应用实例的方法获取全局数据
        app.getUserInfo(function(userInfo){
            //更新数据
            that.setData({
                userInfo:userInfo
            })
            wx.request({
                url:"https://wxapps.lovemojito.com/mApp/getParty.php",
                data: {
                    openid:userInfo.openId,
                    partyId:that.data.partyId,
                    flag:0,
                    jyj:'jyj'
                },
                header: {
                    'content-type': 'application/json'
                },
                success: function(res) {
                    // console.log(res.data)
                    var latitude = res.data.latitude
                    var longitude = res.data.longitude
                    var markers = [{
                        iconPath: "../../images/location.png",
                        id: 0,
                        latitude:latitude,
                        longitude: longitude,
                        width: 30,
                        height: 30
                    }];
                    that.setData({
                        latitude:latitude,
                        longitude: longitude,
                        markers:markers,
                        title:res.data.title,
                        beginTime:res.data.beginTime,
                        beginTimeUnix:res.data.beginTimeUnix,
                        description:res.data.description,
                        locationName : res.data.locationName,
                        locationAddress: res.data.locationAddress,
                        status: res.data.status,
                        isOrganiser:res.data.isOrganiser,
                        isAttender:res.data.isAttender,
                        attender:res.data.attender,
                        attenderNum:res.data.attenderNum,
                        maxMember:res.data.maxMember
                    })
                    console.log(1)
                    console.log(that.data)
                    console.log(2)
                    wx.stopPullDownRefresh()

                }
            })

        })

    }

})